package cn.bouncyslime.graduationProject.nba.service;

import cn.bouncyslime.graduationProject.nba.dao.PlayerDataDao;
import cn.bouncyslime.graduationProject.nba.dao.PlayerInfoDao;
import cn.bouncyslime.graduationProject.nba.dto.*;
import cn.bouncyslime.graduationProject.nba.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @author 张一卓
 * @date 2020/4/7 14:12
 **/
@Service
public class PlayerDataCompareService {

    @Autowired
    PlayerDataDao playerDataDao;

    @Autowired
    PlayerInfoDao playerInfoDao;

    public ResponseData singlePlayerCompare(List<String> playerIdList, String seasonType, String type, String dataType) {
        String methodName = "get" + dataType.substring(0, 1).toUpperCase() + dataType.substring(1);
        Map resultMap = new HashMap();
        List playerList = new ArrayList();
        Set seasonSet = new TreeSet();
        if ("regularSeason".equals(seasonType)) {
            if ("avg".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<RS_PlayerAveragingDto> playerData = playerDataDao.listRSPlayerAveragingData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (RS_PlayerAveragingDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            } else if ("sum".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<RS_PlayerSummaryDto> playerData = playerDataDao.listRSPlayerSummaryData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (RS_PlayerSummaryDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            } else if ("basicAdv".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<PlayerAdvancedBasicDto> playerData = playerDataDao.listRSPlayerAdvancedBasicData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (PlayerAdvancedBasicDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            } else if ("shootingAdv".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<PlayerAdvancedShootingDto> playerData = playerDataDao.listRSPlayerAdvancedShootingData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (PlayerAdvancedShootingDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            }
            resultMap.put("seasonList", new ArrayList(seasonSet));
            resultMap.put("playerList", playerList);
        } else if ("postSeason".equals(seasonType)) {
            if ("avg".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<PS_PlayerAveragingDto> playerData = playerDataDao.listPSPlayerAveragingData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (PS_PlayerAveragingDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            } else if ("sum".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<PS_PlayerSummaryDto> playerData = playerDataDao.listPSPlayerSummaryData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (PS_PlayerSummaryDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            } else if ("basicAdv".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<PlayerAdvancedBasicDto> playerData = playerDataDao.listPSPlayerAdvancedBasicData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (PlayerAdvancedBasicDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            } else if ("shootingAdv".equals(type)) {
                Map playerMap = null;
                for (String playerId : playerIdList) {
                    playerMap = new HashMap();
                    List<PlayerAdvancedShootingDto> playerData = playerDataDao.listPSPlayerAdvancedShootingData(Integer.valueOf(playerId), null);
                    Map map = new HashMap();
                    for (PlayerAdvancedShootingDto dto : playerData) {
                        seasonSet.add(dto.getSeason());
                        try {
                            Method method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            map.put(dto.getSeason(), data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                    playerMap.put("playerId", playerId);
                    playerMap.put("playerCName", playerInfoDao.getPlayerById(Integer.valueOf(playerId)).getPlayerCName());
                    playerMap.put("dataMap", map);
                    playerList.add(playerMap);
                }
            }
            resultMap.put("seasonList", new ArrayList(seasonSet));
            resultMap.put("playerList", playerList);
        }
        return ResponseData.success(resultMap);
    }

    public ResponseData multiplePlayerCompare(List<String> playerIdList, String seasonType, List<String> typeList, List<String> dataTypeList, String season) {
        Map resultMap = new HashMap();
        List<List> resultList = new ArrayList();
//        List<Map> tempList = new ArrayList();
        if ("regularSeason".equals(seasonType)) {
            for (int i = 0; i < playerIdList.size(); i++) {
                Integer playerId = Integer.valueOf(playerIdList.get(i));
                List list = new ArrayList();
//                Map map = new HashMap();
                for (int j = 0; j < typeList.size(); j++) {
                    String type = typeList.get(j);
                    if ("avg".equals(type)) {
                        RS_PlayerAveragingDto dto = null;
                        List<RS_PlayerAveragingDto> playerDataList = playerDataDao.listRSPlayerAveragingData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new RS_PlayerAveragingDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
//                        RS_PlayerAveragingDto dto = playerDataDao.listRSPlayerAveragingData(playerId, season).get(0);
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if ("sum".equals(type)) {
                        RS_PlayerSummaryDto dto = null;
                        List<RS_PlayerSummaryDto> playerDataList = playerDataDao.listRSPlayerSummaryData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new RS_PlayerSummaryDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
//                        RS_PlayerSummaryDto dto = playerDataDao.listRSPlayerSummaryData(playerId, season).get(0);
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if ("basicAdv".equals(type)) {
                        PlayerAdvancedBasicDto dto = null;
                        List<PlayerAdvancedBasicDto> playerDataList = playerDataDao.listRSPlayerAdvancedBasicData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new PlayerAdvancedBasicDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
//                        PlayerAdvancedBasicDto dto = playerDataDao.listRSPlayerAdvancedBasicData(playerId, season).get(0);
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if ("shootingAdv".equals(type)) {
                        PlayerAdvancedShootingDto dto = null;
                        List<PlayerAdvancedShootingDto> playerDataList = playerDataDao.listRSPlayerAdvancedShootingData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new PlayerAdvancedShootingDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
//                        PlayerAdvancedShootingDto dto = playerDataDao.listRSPlayerAdvancedShootingData(playerId, season).get(0);
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (playerInfoDao.getPlayerById(playerId).getPlayerCName() == null) {
                    list.add(playerInfoDao.getPlayerById(playerId).getPlayerEName());
                }
                list.add(playerInfoDao.getPlayerById(playerId).getPlayerCName());
//                tempList.add(map);
                resultList.add(list);
            }
        } else if ("postSeason".equals(seasonType)) {
            for (int i = 0; i < playerIdList.size(); i++) {
                Integer playerId = Integer.valueOf(playerIdList.get(i));
                List list = new ArrayList();
//                Map map = new HashMap();
                for (int j = 0; j < typeList.size(); j++) {
                    String type = typeList.get(j);
                    if ("avg".equals(type)) {
                        PS_PlayerAveragingDto dto = null;
                        List<PS_PlayerAveragingDto> playerDataList = playerDataDao.listPSPlayerAveragingData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new PS_PlayerAveragingDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if ("sum".equals(type)) {
//                        PS_PlayerSummaryDto dto = playerDataDao.listPSPlayerSummaryData(playerId, season).get(0);
                        PS_PlayerSummaryDto dto = null;
                        List<PS_PlayerSummaryDto> playerDataList = playerDataDao.listPSPlayerSummaryData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new PS_PlayerSummaryDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if ("basicAdv".equals(type)) {
                        PlayerAdvancedBasicDto dto = null;
                        List<PlayerAdvancedBasicDto> playerDataList = playerDataDao.listPSPlayerAdvancedBasicData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new PlayerAdvancedBasicDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
//                        PlayerAdvancedBasicDto dto = playerDataDao.listPSPlayerAdvancedBasicData(playerId, season).get(0);
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    } else if ("shootingAdv".equals(type)) {
                        PlayerAdvancedShootingDto dto = null;
                        List<PlayerAdvancedShootingDto> playerDataList = playerDataDao.listPSPlayerAdvancedShootingData(playerId, season);
                        if (playerDataList == null || playerDataList.size() == 0){
                            dto = new PlayerAdvancedShootingDto();
                        }else {
                            dto = playerDataList.get(0);
                        }
//                        PlayerAdvancedShootingDto dto = playerDataDao.listPSPlayerAdvancedShootingData(playerId, season).get(0);
                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
                        // 下面的这个代码之后会通过 sql 的更改来代替
                        Method method = null;
                        try {
                            method = dto.getClass().getDeclaredMethod(methodName);
                            Object data = method.invoke(dto);
                            list.add(data);
                        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
                if (playerInfoDao.getPlayerById(playerId).getPlayerCName() == null) {
                    list.add(playerInfoDao.getPlayerById(playerId).getPlayerEName());
                }
                list.add(playerInfoDao.getPlayerById(playerId).getPlayerCName());
//                tempList.add(map);
                resultList.add(list);
            }
        }
        resultMap.put("dataList", resultList);
//        resultMap.put("")
        return ResponseData.success(resultMap);
    }

//    public ResponseData multiplePlayerCompare(List<String> playerIdList, String seasonType, List<String> typeList, List<String> dataTypeList, String season){
//        Map resultMap = new HashMap();
//        List<List> resultList = new ArrayList();
////        List<Map> tempList = new ArrayList();
//        if ("regularSeason".equals(seasonType)){
//            for (int i=0; i<playerIdList.size(); i++){
//                Integer playerId = Integer.valueOf(playerIdList.get(i));
//                List list = new ArrayList();
////                Map map = new HashMap();
//                for (int j = 0; j<typeList.size(); j++){
//                    String type = typeList.get(j);
//                    if ("avg".equals(type)){
//                        List<RS_PlayerAveragingDto> playerData = playerDataDao.listRSPlayerAveragingData(playerId);
//                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
//                        // 下面的这个代码之后会通过 sql 的更改来代替
//                        for (RS_PlayerAveragingDto dto : playerData){
//                            if (dto.getSeason().equals(season)){
//                                Method method = null;
//                                try {
//                                    method = dto.getClass().getDeclaredMethod(methodName);
//                                    Object data = method.invoke(dto);
//                                    list.add(data);
//                                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//                    }else if("sum".equals(type)){
//                        List<RS_PlayerSummaryDto> playerData = playerDataDao.listRSPlayerSummaryData(playerId);
//                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
//                        // 下面的这个代码之后会通过 sql 的更改来代替
//                        for (RS_PlayerSummaryDto dto : playerData){
//                            if (dto.getSeason().equals(season)){
//                                Method method = null;
//                                try {
//                                    method = dto.getClass().getDeclaredMethod(methodName);
//                                    Object data = method.invoke(dto);
//                                    list.add(data);
//                                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//                    }else if("basicAdv".equals(type)){
//                        List<PlayerAdvancedBasicDto> playerData = playerDataDao.listRSPlayerAdvancedBasicData(playerId);
//                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
//                        // 下面的这个代码之后会通过 sql 的更改来代替
//                        for (PlayerAdvancedBasicDto dto : playerData){
//                            if (dto.getSeason().equals(season)){
//                                Method method = null;
//                                try {
//                                    method = dto.getClass().getDeclaredMethod(methodName);
//                                    Object data = method.invoke(dto);
//                                    list.add(data);
//                                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//                    }else if("shootingAdv".equals(type)){
//                        List<PlayerAdvancedShootingDto> playerData = playerDataDao.listRSPlayerAdvancedShootingData(playerId);
//                        String methodName = "get" + dataTypeList.get(j).substring(0, 1).toUpperCase() + dataTypeList.get(j).substring(1);
//                        // 下面的这个代码之后会通过 sql 的更改来代替
//                        for (PlayerAdvancedShootingDto dto : playerData){
//                            if (dto.getSeason().equals(season)){
//                                Method method = null;
//                                try {
//                                    method = dto.getClass().getDeclaredMethod(methodName);
//                                    Object data = method.invoke(dto);
//                                    list.add(data);
//                                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//                    }
//                }
//                if (playerInfoDao.getPlayerById(playerId).getPlayerCName() == null){
//                    list.add(playerInfoDao.getPlayerById(playerId).getPlayerEName());
//                }
//                list.add(playerInfoDao.getPlayerById(playerId).getPlayerCName());
////                tempList.add(map);
//                resultList.add(list);
//            }
//        }else if("postSeason".equals(seasonType)){
//
//        }
//        resultMap.put("dataList", resultList);
////        resultMap.put("")
//        return ResponseData.success(resultMap);
//    }
}
